package com.web.admin.global.controller;

import com.blue.core.auth.DigestUtil;
import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.web.admin.global.model.User;
import com.web.admin.global.util.HttpContext;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.auth.message.MenuResponse;
import com.web.base.user.exception.UserException;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserLoginLogRequest;
import com.web.base.user.message.UserLoginLogResponse;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.base.valid.UserPassword;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@Http(url = "/index")
public class IndexController
{
	@Autowired
	private MenuFacade menuFacade;
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private AppFacade appFacade;
	@Resource
	private HttpContext httpContext;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public IndexController()
	{
	}
	
	@Http(url = "", method = HttpMethod.GET)
	public View index(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		Map<String, List<MenuResponse>> menuMap = menuFacade.index(user.getId());
		return View.createBuilder().setView("/index/index")
				.put("menuMap", menuMap)
				.build();
	}
	
	@Http(url = "/login_log", method = HttpMethod.GET)
	public View loginLogGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UserLoginLogRequest logRequest = new UserLoginLogRequest();
		logRequest.setUserId(user.getId());
		Page<UserLoginLogResponse> page = userFacade.listLoginPage(logRequest, Const.PAGE_START, Const.PAGE_SIZE);
		Set<Integer> idSet = page.getObjectList().stream().map(UserLoginLogResponse::getAppId).collect(Collectors.toSet());
		Map<Integer, AppResponse> appMap = appFacade.getList(idSet);
		for (UserLoginLogResponse log : page.getObjectList())
		{
			AppResponse response = appMap.get(log.getAppId());
			if (response != null)
			{
				log.setAppName(response.getName());
			}
		}
		return View.createBuilder().setView("/index/login_log")
				.put("page", page)
				.put("map", dictParser.getStringMap())
				.build();
	}

	@Http(url = "/login_log", method = HttpMethod.POST)
	public View loginLogPost(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UserLoginLogRequest logRequest = new UserLoginLogRequest();
		logRequest.setUserId(user.getId());
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<UserLoginLogResponse> page = userFacade.listLoginPage(logRequest, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/index/login_log_grid")
				.put("page", page)
				.build();
	}
	
	@Http(url = "/password", method = HttpMethod.GET)
	public View passwordGet(HttpRequest request)
	{
		return View.createBuilder().setView("/index/password").build();
	}
	
	@Http(url = "/password", method = HttpMethod.POST)
	public void passwordPost(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UserRequest userRequest = request.getPostObject(UserRequest.class, UserPassword.class);
		userRequest.setOldPassword(DigestUtil.sha1(userRequest.getOldPassword()));
		if (!user.getPassword().equals(userRequest.getOldPassword()))
			throw new UserException("旧密码错误");
		
		UserRequest userRequest2 = new UserRequest();
		userRequest2.setId(user.getId());
		userRequest2.setName(user.getName());
		userRequest2.setPassword(userRequest.getPassword());
		UserResponse response = userFacade.update(userRequest2);
		httpContext.setUser(request, User.toUser(response));
	}
	
}
